<?php


namespace app\shop\model;


use app\BaseModel;

class AdminParameterTemplateInfo extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_goods_parameter_template_info';

    /**
     * @param $where
     * @param $goods_id
     * @功能说明:获取模板详情
     * @author lxj
     * @DataTime: 2020/5/19 9:26
     */
    public function getInfo($where,$goods_id=0){
        $list = $this->alias('a')
            ->leftjoin('longbing_card_goods_parameter b','b.id = a.parameter_id')
            ->where($where)
            ->field('a.pid,a.parameter_id,b.name parameter_name')
            ->select()
            ->toArray();
        $list = self::getTree($list,0,0,$goods_id);
        return $list;
    }

    /**
     * @param $data
     * @param $info
     * @功能说明:模板详情新增
     * @author lxj
     * @DataTime: 2020/5/19 9:41
     */
    public function templateInfoAdd($info){
        $info['create_time'] = time();
        $res = $this->insertGetId($info);
        return $res;
    }

    /**
     * @param $where
     * @功能说明:模板数据删除
     * @author lxj
     * @DataTime: 2020/5/19 14:37
     */
    public function templateInfoDel($where){
        $res = $this->where($where)->update(['status'=>-1]);
        return $res;
    }

    /**
     * @param array $arr
     * @param int $pid
     * @param int $step
     * @param int $goods_id
     * @功能说明:数据树状处理
     * @author lxj
     * @DataTime: 2020/5/19 15:01
     */
    public function getTree($arr = [], $pid = 0, $step = 0,$goods_id=0)
    {
        $trees = [];
        $lists = array_values($arr);
        foreach ($lists as $key => $value) {
            if ($value['pid'] == $pid) {
                $child = self::getTree($lists, $value['parameter_id'], $step + 1,$goods_id);
                if($value['pid'] == 0){
                    if(!empty($child)){
                        $value['secondList'] = $child;
                    }else{
                        $value['secondList'][] =['id'=>''] ;
                    }
                    $value['firstId'] = $value['parameter_id'];
                }else{
                    if($goods_id){
                        $parameter_value =AdminGoodsParameterValue::where(['goods_id'=>$goods_id,'parameter_id'=>$value['parameter_id'],'status'=>1])->value('parameter_value');
                        $value['parameter_value'] =$parameter_value??'';
                    }
                    $value['id'] = $value['parameter_id'];
                }
                unset($value['parameter_id']);
                $trees[] = $value;
            }
        }
        return $trees;
    }

    /**
     * @param $where
     * @功能说明:判断该参数是否被某个模板使用
     * @author lxj
     * @DataTime: 2020/5/22 11:44
     */
    public function isUse($where){
        $template_ids = $this->where($where)->column('template_id');
        if($template_ids){
            //查询模板状态
            $template_status = AdminParameterTemplate::where('id','in',$template_ids)->where('status',1)->find();
            if($template_status){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }
}